﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using CoiffeurProject.Screens;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using CoiffeurProject.Utils;
using System.Windows.Media;

namespace CoiffeurProject.UserControls
{
    internal class CustomersTableHeaders
    {
        CustomerScreen screen;
        Image name_und, phone_und, address_und, age_und, returned_und;

        Grid main_container;
        Button name, phone, address, age, returned;

        internal CustomersTableHeaders(CustomerScreen screen, int row, int col, int colspan, int rowspan)
        {
            this.screen = screen;            

            main_container = new Grid()
            {
                Width = 1200,
                Height = 45,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(50,18,0,0)
            };

            Grid.SetColumn(main_container, col);
            Grid.SetRow(main_container, row);
            if (colspan > 0) Grid.SetColumnSpan(main_container, colspan);
            if (rowspan > 0) Grid.SetRowSpan(main_container, rowspan);

            LoadImages();
            LoadButtons();
        }

        private void LoadButtons()
        {
            name = new Button()
            {
                Width = 210,
                Height = 45,
                Margin = new Thickness(9, 0, 0, 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Content = new Image() { Source = new BitmapImage(new Uri(ImagesPath.Blank)), Stretch = Stretch.Fill }
            };

            name.SetResourceReference(Button.StyleProperty, "NoChromeButton");

            name.MouseEnter += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseEnter(0); });
            name.MouseLeave += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseLeave(0); });
            name.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e) { screen.FilterCustomers(CustomerScreen.FilterParameter.Name); });
            main_container.Children.Add(name);

            phone = new Button()
            {
                Width = 210,
                Height = 45,
                Margin = new Thickness(240, 0, 0, 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Content = new Image() { Source = new BitmapImage(new Uri(ImagesPath.Blank)), Stretch = Stretch.Fill }
            };

            phone.SetResourceReference(Button.StyleProperty, "NoChromeButton");

            phone.MouseEnter += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseEnter(1); });
            phone.MouseLeave += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseLeave(1); });
            phone.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e) { screen.FilterCustomers(CustomerScreen.FilterParameter.Phone); });
            main_container.Children.Add(phone);

            address = new Button()
            {
                Width = 210,
                Height = 45,
                Margin = new Thickness(451, 0, 0, 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Content = new Image() { Source = new BitmapImage(new Uri(ImagesPath.Blank)), Stretch = Stretch.Fill }
            };

            address.SetResourceReference(Button.StyleProperty, "NoChromeButton");

            address.MouseEnter += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseEnter(2); });
            address.MouseLeave += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseLeave(2); });
            address.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e) { screen.FilterCustomers(CustomerScreen.FilterParameter.Address); });
            main_container.Children.Add(address);

            age = new Button()
            {
                Width = 210,
                Height = 45,
                Margin = new Thickness(690, 0, 0, 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Content = new Image() { Source = new BitmapImage(new Uri(ImagesPath.Blank)), Stretch = Stretch.Fill }
            };

            age.SetResourceReference(Button.StyleProperty, "NoChromeButton");

            age.MouseEnter += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseEnter(3); });
            age.MouseLeave += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseLeave(3); });
            age.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e) { screen.FilterCustomers(CustomerScreen.FilterParameter.Age); });
            main_container.Children.Add(age);

            returned = new Button()
            {
                Width = 210,
                Height = 45,
                Margin = new Thickness(937, 0, 0, 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Content = new Image() { Source = new BitmapImage(new Uri(ImagesPath.Blank)), Stretch = Stretch.Fill }
            };

            returned.SetResourceReference(Button.StyleProperty, "NoChromeButton");

            returned.MouseEnter += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseEnter(4); });
            returned.MouseLeave += new MouseEventHandler(delegate(object sender, MouseEventArgs e) { MouseLeave(4); });
            returned.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e) { screen.FilterCustomers(CustomerScreen.FilterParameter.Returned); });
            main_container.Children.Add(returned);
        }

        private void LoadImages()
        {
            name_und = new Image() 
            { 
                Height = 3,
                Width = 178,
                Stretch = System.Windows.Media.Stretch.Fill,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(17,35,0,0),
                Source = new BitmapImage(new Uri(@"pack://application:,,,/CoiffeurProject;component/Images/Customer/TableUnderlines/name_underline.PNG")),
                Visibility = Visibility.Hidden
            };

            phone_und = new Image()
            {
                Height = 3,
                Width = 93,
                Stretch = System.Windows.Media.Stretch.Fill,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(281, 35, 0, 0),
                Source = new BitmapImage(new Uri(@"pack://application:,,,/CoiffeurProject;component/Images/Customer/TableUnderlines/phone_underline.PNG")),
                Visibility = Visibility.Hidden
            };

            address_und = new Image()
            {
                Height = 3,
                Width = 95,
                Stretch = Stretch.Fill,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(506, 35, 0, 0),
                Source = new BitmapImage(new Uri(@"pack://application:,,,/CoiffeurProject;component/Images/Customer/TableUnderlines/address_underline.PNG")),
                Visibility = Visibility.Hidden
            };

            age_und = new Image()
            {
                Height = 3,
                Width = 52,
                Stretch = Stretch.Fill,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(768, 35, 0, 0),
                Source = new BitmapImage(new Uri(@"pack://application:,,,/CoiffeurProject;component/Images/Customer/TableUnderlines/age_underline.PNG")),
                Visibility = Visibility.Hidden
            };

            returned_und = new Image()
            {
                Height = 3,
                Width = 100,
                Stretch = Stretch.Fill,
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(1013, 35, 0, 0),
                Source = new BitmapImage(new Uri(@"pack://application:,,,/CoiffeurProject;component/Images/Customer/TableUnderlines/returned_underline.PNG")),
                Visibility = Visibility.Hidden
            };

            main_container.Children.Add(name_und);
            main_container.Children.Add(phone_und);
            main_container.Children.Add(address_und);
            main_container.Children.Add(age_und);
            main_container.Children.Add(returned_und);
        }

        private void MouseEnter(int i)
        {
            switch (i)
            { 
                case 0:
                    name_und.Visibility = Visibility.Visible;
                    break;
                case 1:
                    phone_und.Visibility = Visibility.Visible;
                    break;
                case 2:
                    address_und.Visibility = Visibility.Visible;
                    break;
                case 3:
                    age_und.Visibility = Visibility.Visible;
                    break;
                default:
                    returned_und.Visibility = Visibility.Visible;
                    break;
            }
        }

        private void MouseLeave(int i)
        {
            switch (i)
            {
                case 0:
                    name_und.Visibility = Visibility.Hidden;
                    break;
                case 1:
                    phone_und.Visibility = Visibility.Hidden;
                    break;
                case 2:
                    address_und.Visibility = Visibility.Hidden;
                    break;
                case 3:
                    age_und.Visibility = Visibility.Hidden;
                    break;
                default:
                    returned_und.Visibility = Visibility.Hidden;
                    break;
            }
        }

        public Grid MainContainer
        {
            get { return main_container; }
        }
    }
}
